<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/common/taglib.jsp" %>    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script>
	//# sourceURL=orgGrantRoleList.js
	
	var isInherit = false;
	
	$(document).ready(function() {
		$("#ckIsInherit").change(function() {
			isInherit = $("#ckIsInherit").is(':checked');
			mtr_pageIndex = 1;
			loadRoleGrid(grant_org_treeNodeId);
		});
	});

// 	var selGrantRoleId = null;
// 	var selGrantRoleName = null;

// 	$(document).ready(function() {

// 		// 选择表格行
// 		$("#gridGrantRole table > tbody > tr").click(function() {
// 			$("#gridGrantRole table > tbody > tr").removeClass('info');
// 			$(this).addClass('info');
// 			selGrantRoleId = $(this)[0].id;
// 			selGrantRoleName = $(this).find("td").eq(1).text();
// 		});

// 	});
	
	function selectRole() {
		if (check_addRole() == false) return;
		
		$('#dialog_main').modal({
			keyboard: false,
			remote: '${ctx}/org/role/select.action'
		});
	}
	
	function onSelectRoleEvent(roleId) {
		$('#dialog_main').modal('hide');		
		//loadPage(appId);	
		//alert("roleId: " + roleId);
		addGrantRole(roleId);
	}	
	
	function addGrantRole(roleId) {
		var url = "${ctx}/org/grant/add.action?roleId=" + roleId + "&orgId=" + grant_org_treeNodeId;
		$.get(url, function(data) {
			if(data == "success") {
				loadRoleGrid(grant_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});
	}	

	function deleteGrantRole() {
		if (checkSelected() == false) return;
		
		if(confirm('确定要删除"' + selGrantRoleName + '"吗？') == false) {
			return;
		}

		var url = "${ctx}/org/grant/delete.action?roleId=" + selGrantRoleId + "&orgId=" + grant_org_treeNodeId;
		$.get(url, function(data) {			    		
			if(data == "success") {
				loadRoleGrid(grant_org_treeNodeId);
			} else {
				alert("错误: \n" + data);
			}
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) { 
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});			
	}

	function checkSelected() {
		if (selGrantRoleId == null) {
			alert("请选择表格行");
			return false;
		} else {
			return true;
		}
	}
	
	function check_addRole() {		
		if (grant_org_treeNodeId == null) {
			alert("请选择组织节点");
			return false;			
		} else if (grant_org_treeNodeId == 0) {
			alert("根节点不能授权");
			return false;						
		}		
		return true;
	}

	function dialogSubmitSuccess() {
		$('#dialog_main').modal('hide');
		loadRoleGrid(grant_org_treeNodeId);
	}

    function refreshPage() {
		var url = "/org/person/manage.action";
		load_main_content(url);
    }
    
    function loadRoleGrid(orgId) {
    	if (orgId == null) return;
    	
    	var url = "/org/grant/grid.action?orgId=" + orgId + "&isInherit=" + isInherit + 
    			"&pageIndex=" + mtr_pageIndex;
    	
		$.get("${ctx}" + url, function(data) {
			$("#gridGrantRolePage").empty().append(data);
		})
		.error(function(xmlHttpRequest, textStatus, errorThrown) {
			  alert("异常： " + xmlHttpRequest.status + ":" + errorThrown);
		});
    }
    
    var mtr_pageIndex = 1;
	function onPageClick(n) {
		mtr_pageIndex = n;
		loadRoleGrid(grant_org_treeNodeId);
	}    

</script>

</head>
<body>

<div>
	<h3>组织授权</h3>
</div>

<div class="col-md-2 column" style="border-right:1px solid #ccc;">
	<script>
		var grant_org_setting = {
			check: {
				enable: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
	        callback: {
	            onClick: person_org_onTreeClick
	        }
		};
		
		var grant_org_treeNodeId = null;
		var grant_org_treeNodeName = null;
		
		function person_org_onTreeClick(event, treeId, treeNode, clickFlag) {
			grant_org_treeNodeId = treeNode.id;
			grant_org_treeNodeName = treeNode.name;
			
			//alert(grant_org_treeNodeId + ":" + grant_org_treeNodeName);
			mtr_pageIndex = 1;
			loadRoleGrid(grant_org_treeNodeId);
		}
	
		var grant_org_Nodes = ${listOrganization};
	
		$(document).ready(function(){
			$.fn.zTree.init($("#grant_orgTreeView"), grant_org_setting, grant_org_Nodes);
		});
		
	</script>
	<div class="row clearfix">
	<ul id="grant_orgTreeView" class="ztree"></ul>
	<br/>
	</div>
</div>


<div class="col-md-10 column">
	<nav class="navbar navbar-default" role="navigation">
	  <ul class="nav navbar-nav navbar-left">
	    <li>
	      <a href="javascript:selectRole()">添加</a></li>
	    <li>
	      <a href="javascript:deleteGrantRole()">删除</a></li>
	      
	    <li class="dropdown">
	      <a href="#" class="dropdown-toggle" data-toggle="dropdown">选项
	        <b class="caret"></b></a>
	      <ul class="dropdown-menu">
	        <li>&nbsp;<input id="ckIsInherit" type="checkbox">&nbsp;显示继承</li>        
	      </ul>
	    </li>	      
	  </ul>
	</nav>
	
	<div id="gridGrantRolePage" class="clearfix">
	<%@ include file="/WEB-INF/view/modules/org/organizationGrantGrid.jsp" %>
	</div>
	
<%-- 	<div id="gridGrantRole" class="table-responsive">
	  <table class="table table-hover table-bordered">
	    <thead>
	      <tr>        
	      	<th>所属应用</th>
	        <th>角色名称</th>     
	      </tr>
	    </thead>
	    <tbody>
		<c:forEach items="${listRole}" var="node" varStatus="status">		
			<tr id='${node.roleId}'>
			<td>${node.appName}</td>	
			<td>${node.roleName}</td>
			</tr>
		</c:forEach>
	    </tbody>
	  </table>
	</div> --%>

<!-- 	<div class="clearfix">
	  <div class="col-md-6 column" style="background-color:white;text-align: left;">当前显示1~20条,共161条</div>
	  <div class="col-md-6 column" style="background-color:white;text-align: right;">
	    <ul class="pagination" style="margin-top: 0px;margin-bottom: 10px;">
	      <li>
	        <a href="#">&laquo;</a></li>
	      <li class="active">
	        <a href="#">1</a></li>
	      <li>
	        <a href="#">2</a></li>
	      <li>
	        <a href="#">3</a></li>
	      <li>
	        <a href="#">4</a></li>
	      <li>
	        <a href="#">5</a></li>
	      <li>
	        <a href="#">..</a></li>
	      <li>
	        <a href="#">&raquo;</a></li>
	    </ul>
	  </div>
	</div> -->
	
</div>

</body>
</html>